// service层
import User from "../model/User"

class LoginService {
  getUserById(id: number) {
    return User.findByPk(id)
  }

  getUserByName(name: string) {
    return User.findOne({
      where: {
        account: name
      }
    })
  }

  getUserListByPage(page: number = 1, limit: number = 15) {
    /* 关键在于对应sql语句
        SELECT * FROM users
        LIMIT 3
        OFFSET 3
     */
    return User.findAndCountAll({
      limit: limit,
      offset: (page - 1) * limit
    })
  }

  addUser(admin: any) {
    return User.create(admin)
  }

  updateUser(id: number, admin: any) {
    return User.update(admin, { where: { id } })
  }

  deleteUser(id: number) {
    return User.destroy({ where: { id } })
  }
}

export default new LoginService